package main
import (
	"fmt"
	"net"
	"time"
	"os"
	"io"
)

func main(){
	defer time.Sleep(time.Second)
	filename := `K:\UE从零开始搭建彩色环境-上部分.wmv`
	info, err := os.Stat(filename)
	if err != nil{
		fmt.Println("Stat err : ", err)
		return
	}
	
	/*第一步，连接服务器*/
	conn, err2 := net.Dial("tcp", "127.0.0.1:8000")
	if err2 != nil{
		fmt.Println("连接 tcp 127.0.0.1:8000 失败. err : ", err2)
		return
	}
	
	var sum int
	sum, err = conn.Write([]byte(info.Name()))
	if err != nil{
		fmt.Println("Write err : ", err)
		return
	}
	
	buf := make([]byte, 1024)
	sum, err = conn.Read(buf)
	if err != nil{
		fmt.Println("文件发送失败,不能读取文件，err : ", err)
		return
	}
	if "OK" == string(buf[:sum]){
		f, err := os.Open(filename)
		if err != nil{
			fmt.Println("文件读取失败，err : ", err)
			return
		}
		defer f.Close()
		for{
			sum, err = f.Read(buf)
			if err != nil{
				if err == io.EOF{
					fmt.Println("文件发送完毕")
					break
				}else{
					fmt.Println("文件发送失败")
					return
				}
			}
			conn.Write(buf[:sum])
		}
	}else{
		fmt.Println("服务器拒绝接收文件！")
		return
	}
}